from playwright.sync_api import sync_playwright
import pytest
from pytest import Item
import allure

# 本地插件注册
pytest_plugins = ['plugins.pytest_playwright'] # noqa
def pytest_runtest_call(item: Item): # noqa
    # 动态添加测试类的 allure.feature() if item.parent._obj.__doc__: # noqa
    allure.dynamic.feature(item.parent._obj.__doc__) # noqa
    # 动态添加测试用例的title 标题 allure.title() if item.function.__doc__: # noqa
    allure.dynamic.title(item.function.__doc__) # noqa

def pytest_runtest_call(item: Item): # noqa
    # 动态添加测试类的 allure.feature()
    if item.parent._obj.__doc__: # noqa
        allure.dynamic.feature(item.parent._obj.__doc__) # noqa
    # 动态添加测试用例的title 标题 allure.title()
    if item.function.__doc__: # noqa
        allure.dynamic.title(item.function.__doc__) # noqa

@pytest.fixture(scope="session")
def context_chrome():
    p = sync_playwright().start()
    browser = p.chromium.launch(headless=False)
    context = browser.new_context()
    yield context
    # 实现用例后置
    context.close()
    browser.close()
    p.stop()